<?php

namespace Xin\LaravelFortify\Plugin;

use Illuminate\Support\Str;

trait WithCurrentPlugin
{
    /**
     * @var string
     */
    protected string $pluginName = '';

    /**
     * @var string
     */
    protected string $pluginNameLower = '';

    /**
     * 获取当前插件名称
     * @param bool $snake
     * @return string
     */
    public function getCurrentPluginName(bool $snake = false)
    {
        if (!$this->pluginName) {
            $name = explode("\\", get_class($this), 3)[1];

            $this->pluginName = $name;
            $this->pluginNameLower = Str::snake($name);
        }

        return $snake ? $this->pluginNameLower : $this->pluginName;
    }

    /**
     * 获取当前插件
     * @return \Nwidart\Modules\Module
     */
    public function getCurrentPlugin()
    {
        $pluginName = $this->getCurrentPluginName();

        return Plugin::info($pluginName);
    }

    /**
     * 获取当前插件目录
     * @param string $path
     * @return string
     */
    protected function basePath(string $path = '')
    {
        return Plugin::basePath($this->getCurrentPluginName(), $path);
    }

    /**
     * 获取当前插件配置目录
     * @param string $path
     * @return string
     */
    protected function configPath(string $path = '')
    {
        return $this->basePath(join_paths('config', $path));
    }

    /**
     * 获取当前插件数据库目录
     * @param string $path
     * @return string
     */
    protected function databasePath(string $path = '')
    {
        return $this->basePath(join_paths('database', $path));
    }
}
